package cn.tedu.ivos.audit.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.dto.AuditQuery;
import cn.tedu.ivos.audit.pojo.dto.AuditSaveParam;
import cn.tedu.ivos.audit.pojo.entity.Audit;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.base.enums.AuditStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
@Transactional
public class AuditServiceImpl implements AuditService {
    @Autowired
    AuditMapper auditMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ApplicationMapper applicationMapper;



    @Override
    public void insertAudit(Application application) {
        log.debug("为当前申请单生成对应的审批单数据业务,参数:{}",application);
        List<Long> userIdList=application.getAuditUserIdList();
        for (int i=0;i<userIdList.size();i++){
            Audit audit=new Audit();
            audit.setApplicationId(application.getId());
            audit.setAuditUserId(userIdList.get(i));
            audit.setAuditSort(i);
            if (i==0){
                audit.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
            }else {
                audit.setAuditStatus(AuditStatusEnum.PENDING.getCode());
            }
            audit.setCreateTime(new Date());
            auditMapper.insert(audit);
        }
    }

    @Override
    public List<AuditVO> selectAuditByApplicationId(Long id) {
        return auditMapper.selectAuditByApplicationId(id);
    }

    @Override
    public List<AuditVO> selectAudit(AuditQuery auditQuery) {
        log.debug("查询审批单业务,参数:{}",auditQuery);
        List<AuditVO> list=auditMapper.selectAudit(auditQuery);
        //        遍历每一个审批对象,依次为每一个审批对象补充审批人信息
        for (int i=0;i<list.size();i++){
            AuditVO auditVO=list.get(i);
            assignAudiUserList(auditVO);
        }
        return list;
    }

    @Override
    public void updateAudit(AuditSaveParam auditSaveParam) {
        log.debug("通过审批单,参数:{}",auditSaveParam);
        Audit audit=new Audit();
        BeanUtils.copyProperties(auditSaveParam,audit);
        audit.setUpdateTime(new Date());
//        准备当前审批对应申请单对象
        Application application=new Application();
        application.setId(audit.getApplicationId());
        application.setUpdateTime(new Date());
//        判断当前时通过还是驳回
        if (audit.getAuditStatus().equals(AuditStatusEnum.AUDITED.getCode())){
            //更新当前的审批单
            auditMapper.update(audit);
//            继续查是否有其他审批单:其他审批单与当前审批单对应同一申请单
            AuditQuery auditQuery=new AuditQuery();
            auditQuery.setApplicationId(audit.getApplicationId());
//            根据申请单id查询当前申请单对应的未审批的审批单
            Integer count=auditMapper.selectRestAuditCount(auditQuery);  //查询在该审批单下剩余的审批单数量
            log.debug("剩余审批单数量：参数{}",count);
            if (count>0){ //有剩余的审批单
//                下一条审批数据是当前审批次序+1
                auditQuery.setAuditSort(audit.getAuditSort()+1);
//                根据申请单id与审批单的次序查询下一条审批单
                List<AuditVO> auditVOList=auditMapper.selectAudit(auditQuery);
                if (auditVOList !=null && auditVOList.size()>0){
                    AuditVO auditVO=auditVOList.get(0);
//                    创建一个空对象，代表第二个审批单对象用为更新
                    Audit audit2=new Audit();
                    audit2.setId(auditVO.getId());//这个审批单就是第二个审批单
//                    设置第二条审批单的状态为待我审核
                    audit2.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
                    auditMapper.update(audit2);
                }
//                设置申请单的状态为 审核中
                application.setStatus(ApplicationStatusEnum.AUDIT.getCode());  //申请单状态变为待审批中
                applicationMapper.update(application);

            }else {
//                没有未审核的数据
                application.setStatus(ApplicationStatusEnum.AUDITED.getCode());
                applicationMapper.update(application);
            }
        }else{ //审批驳回
            audit.setAuditStatus(AuditStatusEnum.REJECT.getCode());
            auditMapper.update(audit);
            AuditQuery auditQuery=new AuditQuery();
            auditQuery.setApplicationId(audit.getApplicationId());
            /*auditQuery.setAuditStatus(AuditStatusEnum.PENDING.getCode());
            Integer count=auditMapper.selectRestAuditCount(auditQuery);
            if (count>0){
                auditMapper.deleteAudit(auditQuery);

            }*/
            List<AuditVO> auditVOList=auditMapper.selectAudit(auditQuery);
//            判断是否查到了审批单列表
            if(auditVOList !=null && auditVOList.size()>0){
                for (int i=0;i<auditVOList.size();i++){
                    AuditVO auditVO=auditVOList.get(i);
                    if (AuditStatusEnum.PENDING.getCode().equals(auditVO.getAuditStatus())){
                        auditMapper.deleteById(auditVO.getId());
                    }
                }
            }


            application.setStatus(ApplicationStatusEnum.REJECT.getCode());
            application.setRejectReason(auditSaveParam.getRejectReason());
            applicationMapper.update(application);

        }


    }

    private void assignAudiUserList(AuditVO auditVO) {
        List<String> auditUsernameList=new ArrayList<>();
        List<Long> auditUserIdList=new ArrayList<>();
        List<AuditVO> auditVOList=auditMapper.selectAuditByApplicationId(auditVO.getApplicationId());
        for (int i=0;i<auditVOList.size();i++){
            Long AuditUserId=auditVOList.get(i).getAuditUserId();
            auditUserIdList.add(AuditUserId);
            UserVO userVO=userMapper.selectUserById(AuditUserId);
            auditUsernameList.add(userVO.getUsername());
        }
        StringJoiner stringJoiner=new StringJoiner(",");
        for (String username:auditUsernameList){
            stringJoiner.add(username);
        }

        auditVO.setAuditUsernameList(stringJoiner.toString());
        auditVO.setAuditUserIdList(auditUserIdList);




    }


}
